﻿using System;
using System.ComponentModel;
using System.Data;
using System.Windows;
using ReflectionStudio.Core.Events;
namespace WPFExcelReport
{
    public partial class TGTTKD02
    {
        public TGTTKD02()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VKDSTAMM();
            this.DataContext = MV;
            InitializeComponent();
        }
        public override void ImportExcel(object sender, RoutedEventArgs e)
        {
            if (cDR == null)
                return;
            try
            {
                cDR.DocType = "LOADEXCEL";
                // cDR.Root = @"C:\IntexReport\Data\KD\EXCEL"; 
                cDR.DESTFOLDER = @"C:\IntexReport\Data\KD\EXCEL";
                if (!System.IO.Directory.Exists(cDR.DESTFOLDER))
                {
                    throw new Exception(cDR.DESTFOLDER + " 不存在");
                }
                cDR.PRINT(null);
                SResult = cDR.SetExchange;
                if (SResult.Tables.Count > 0)
                {
                    TBMain.Clear();
                    INFO = SResult.Tables[0].Rows.Count.ToString() + "行数据已经读入";
                    dwData = TBMain.NewRow();
                    TMV.SetUpNewRow(ref dwData);
                    foreach (DataRow row in SResult.Tables[0].Rows)
                    {                        
                        dwData["KDKUNDNR"] = Helpers.Helper.Right("00000000" + row["KUNDNR"].ToString(), 8);
                        dwData["KDKUNDNA"] = Helpers.Helper.ConvertGB2312To8859P1(row["KUNDNA"].ToString()).PadRight(8).Substring(0, 8);
                        dwData["KDKNAME"] = Helpers.Helper.ConvertGB2312To8859P1(row["KUNDNA"].ToString()).PadRight(15).Substring(0, 15);
                        dwData["KDNAME1"] = Helpers.Helper.ConvertGB2312To8859P1(row["KUNDNA"].ToString()).PadRight(40).Substring(0, 40);
                        TMV.DeleteKDSTAMM(row);
                        TMV.BATCHWB14(dwData);
                    }
                    TMV.POSTCHG();
                    QueryCommand(null, null);
                }
                else
                {
                    MessageBox.Show("确认存在" + @"C:\IntexReport\Data\ITX\EXCEL\ITXEX-XXXX.xls");
                }
                vwMaster.View.MoveCurrentToFirst();
                while (true)
                {
                    vwMaster.View.MoveCurrentToNext();
                    if (vwMaster.View.IsCurrentAfterLast)
                        break;
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            finally
            {

            }

        }
        public override void QueryCommand(object sender, RoutedEventArgs e)
        {
            try
            {

                dwData = TBMain.NewRow();
                dwData["KDMANDANT"] = app.Mandant;
                dwData["KDKUNDNR"] = " AND KDKUNDNR like '" + KUNDNR + "%'";
                dwData["KDKUNDNA"] = " AND (KDKUNDNA LIKE '%" + ART.Trim() + "%' ";
                dwData["KDNAME1"] = " or KDNAME1 LIKE '%" + ART.Trim() + "%') ";
                SResult = TMV.QKD05(dwData);
                MergeData(SResult, false);
                IsSaved = false;
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
    }
}
